package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_customers")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Customer> list = customerService.list();
            PageInfo<Customer> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_customers_by")
    public String getByCondition(Customer customer, PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());

            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(customer.getId()), Customer::getId, customer.getId())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerAge()), Customer::getCustomerAge, customer.getCustomerAge())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerSex()), Customer::getCustomerSex, customer.getCustomerSex())
                    .like(!ObjectUtils.isEmpty(customer.getCustomerName()), Customer::getCustomerName, customer.getCustomerName())
                    .eq(!ObjectUtils.isEmpty(customer.getBuildingNo()), Customer::getBuildingNo, customer.getBuildingNo());
            List<Customer> customers = customerService.list(queryWrapper);
            PageInfo<Customer> pageInfo1 = new PageInfo<>(customers);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_customer")
    public String addOne(@RequestBody Customer customer){
        try {
            customerService.save(customer);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getcustomerby_id")
    public String getById(@RequestParam("customerid") Integer customerid){
        try {
            Customer customer = customerService.getById(customerid);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifycustomerby_id")
    public String modifyById(@RequestBody Customer customer){
        try {
            // 确保日期格式正确
            if (customer.getCheckinDate() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateStr = sdf.format(customer.getCheckinDate());
                customer.setCheckinDate(sdf.parse(dateStr));
            }
            customerService.updateById(customer);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removecustomer_by_id")
    public String removeByid(@RequestParam("customerid") Integer customerid) {
        try {
            customerService.removeById(customerid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

}
